//
//  ForgetPasswordVC.m
//  liBaQiang
//
//  Created by 奇林刘 on 2018/11/9.
//  Copyright © 2018年 liu. All rights reserved.
//

#import "ForgetPasswordVC.h"
#import "CheckVerificationPicView.h"
#import "ChangePasswordVC.h"

@interface ForgetPasswordVC ()
@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UITextField *veriCodeField;
@property (weak, nonatomic) IBOutlet UIButton *sendVeriCodeButton;
@property (weak, nonatomic) IBOutlet UIButton *nextStepButton;
@property (weak, nonatomic) IBOutlet Liu70CountDownLabel *countDownLabel;

@property (strong, nonatomic) CheckVerificationPicView *veriPicView;

@end

@implementation ForgetPasswordVC

#pragma mark - Construction Script
- (void)configSelf {
    self.title = @"验证手机";
    self.view.fp_backgroundColor(HexadecimalColor(@"#F5F6F8"));
}

- (void)layoutComponents {
    [self.phoneField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(RPX(375)));
        make.height.equalTo(@(RPX(49)));
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(RPX(15));
    }];
    
    [self.veriCodeField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(RPX(375)));
        make.height.equalTo(@(RPX(49)));
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.phoneField.mas_bottom).offset(RPX(11));
    }];
    
    [self.sendVeriCodeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(RPX(-15));
        make.centerY.equalTo(self.veriCodeField);
    }];
    
    [self.countDownLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.sendVeriCodeButton);
    }];
    
    [self.nextStepButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.veriCodeField.mas_bottom).offset(RPX(20));
        make.width.equalTo(@(RPX(345)));
        make.height.equalTo(@(RPX(44)));
    }];
}

- (void)configComponents {
    self.phoneField.font = RPXFont(15);
    self.phoneField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, RPX(15), 0)];
    self.phoneField.leftViewMode = UITextFieldViewModeAlways;
    self.phoneField.backgroundColor = HexadecimalColor(@"#FFFFFF");
    self.veriCodeField.font = RPXFont(15);
    self.veriCodeField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, RPX(15), 0)];
    self.veriCodeField.leftViewMode = UITextFieldViewModeAlways;
    self.veriCodeField.backgroundColor = HexadecimalColor(@"#FFFFFF");
    self.sendVeriCodeButton.titleLabel.font = RPXFont(14);
    self.countDownLabel.text = @"";
    self.countDownLabel.textColor = HexadecimalColor(@"2457B5");
    self.nextStepButton.titleLabel.font = RPXFont(16);
    self.nextStepButton.fp_backgroundColor(HexadecimalColor(@"#2D5898")).fp_cornerRadius(RPX(4));
    [self.nextStepButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}

#pragma mark - Events
- (IBAction)onSendVeriCodeButtonTap:(id)sender {
    if (self.phoneField.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请先填写手机号码！"];
        return;
    }
    
    [self.view addSubview:self.veriPicView = [[CheckVerificationPicView alloc] init]];
    [self.veriPicView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    
    self.veriPicView.okButton.fp_addTargetActionForControlEvents(self, @selector(onPicVeriOKButtonTap), UIControlEventTouchUpInside);
}

- (void)onPicVeriOKButtonTap {
    [LBQRequestMobileLoginSendCode goRequestWithMobile:self.phoneField.text
                                              img_code:self.veriPicView.textField.text
                                                  type:LBQRequestMobileLoginSendCodeTypeFindBack
                                             verify_id:self.veriPicView.veriID
                                               success:^(NSURLSessionDataTask *task, id  _Nullable responseObject) {
                                                   self.sendVeriCodeButton.hidden = YES;
                                                   [self.countDownLabel startCountDownToEndDate:[NSDate dateWithTimeIntervalSinceNow:60] finish:^{
                                                       self.sendVeriCodeButton.hidden = NO;
                                                   }];
                                               }
                                               failure:^(NSURLSessionDataTask *task, id  _Nullable responseObject) {
                                                   
                                               }];
    [self.veriPicView removeFromSuperview];
}

- (IBAction)onNextStepButtonTap:(id)sender {
    [LBQRequestMobileLoginForgot goRequestWithMobile:self.phoneField.text
                                                code:self.veriCodeField.text
                                             success:^(NSURLSessionDataTask *task, id  _Nullable responseObject) {
                                                 NSLog(@"success");
                                                 ChangePasswordVC *vc = [[ChangePasswordVC alloc] initWithResetToken:responseObject[@"reset_token"]];
                                                 [self.navigationController pushViewController:vc animated:YES];
                                             }
                                             failure:^(NSURLSessionDataTask *task, id  _Nullable responseObject) {
                                                 NSLog(@"fail");
                                             }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [HandyMethods endEditing];
}

@end
